"""A unittest for the player_data_parser module.
"""

__author__ = 'akshaylal@gmail.com'

import logging
import re

import mox
import unittest

import player_data_parser


class PlayerDataParserTest(unittest.TestCase):

  def setUp(self):
    self.mox = mox.Mox()
    self.fake_data = 'fake-data'

  def tearDown(self):
    pass

  def testParsePlayerStatsPassNameInSingleQuotes(self):
    fake_player_data = (
        '[1234,\'mighty mouse\',1.2,[[[\'size\',[123]],[\'model\',[456]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertTrue(data_parser._ParsePlayerStats(fake_player_data))

    # Lets verify that the map got updated accurately.
    self.assertEquals(1, len(data_parser.player_stats_map))
    self.assertEquals([1234], data_parser.player_stats_map.keys())
    self.assertEquals(3, len(data_parser.player_stats_map[1234]))
    self.assertEquals('mighty mouse',
                      data_parser.player_stats_map[1234]['name'])
    self.assertEquals('123',
                      data_parser.player_stats_map[1234]['size'])
    self.assertEquals('456',
                      data_parser.player_stats_map[1234]['model'])

    self.mox.VerifyAll()

  def testParsePlayerStatsPassNameInDoubleQuotes(self):
    fake_player_data = (
        '[1234,"mighty mouse",1.2,[[[\'size\',[123]],[\'model\',[456]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertTrue(data_parser._ParsePlayerStats(fake_player_data))

    # Lets verify that the map got updated accurately.
    self.assertEquals(1, len(data_parser.player_stats_map))
    self.assertEquals([1234], data_parser.player_stats_map.keys())
    self.assertEquals(3, len(data_parser.player_stats_map[1234]))
    self.assertEquals('mighty mouse',
                      data_parser.player_stats_map[1234]['name'])
    self.assertEquals('123',
                      data_parser.player_stats_map[1234]['size'])
    self.assertEquals('456',
                      data_parser.player_stats_map[1234]['model'])

    self.mox.VerifyAll()

  def testParsePlayerStatsPassSpacesBetweenElements(self):
    fake_player_data = (
        '[1234, "mighty mouse", 1.2,[[[\'size\', [123]],[\'model\',[456]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertTrue(data_parser._ParsePlayerStats(fake_player_data))

    # Lets verify that the map got updated accurately.
    self.assertEquals(1, len(data_parser.player_stats_map))
    self.assertEquals([1234], data_parser.player_stats_map.keys())
    self.assertEquals(3, len(data_parser.player_stats_map[1234]))
    self.assertEquals('mighty mouse',
                      data_parser.player_stats_map[1234]['name'])
    self.assertEquals('123',
                      data_parser.player_stats_map[1234]['size'])
    self.assertEquals('456',
                      data_parser.player_stats_map[1234]['model'])

    self.mox.VerifyAll()

  def testParsePlayerStatsFailEmptyPlayerData(self):
    fake_player_data = ''

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertFalse(data_parser._ParsePlayerStats(fake_player_data))
    self.assertEquals(0, len(data_parser.player_stats_map))
    self.mox.VerifyAll()

  def testParsePlayerStatsFailInvalidPlayerId(self):
    fake_player_data = (
        '["this-will-fail", "mouse", 1.2,[[[\'size\', [123]],[\'model\',[4]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertFalse(data_parser._ParsePlayerStats(fake_player_data))
    self.assertEquals(0, len(data_parser.player_stats_map))
    self.mox.VerifyAll()

  def testParsePlayerStatsFailInvalidPlayerName(self):
    fake_player_data = (
        '[1234, 1234, 1.2,[[[\'size\', [123]],[\'model\',[4]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertFalse(data_parser._ParsePlayerStats(fake_player_data))
    self.assertEquals(0, len(data_parser.player_stats_map))
    self.mox.VerifyAll()

  def testParsePlayerStatsFailDuplicatePlayerId(self):
    fake_player_data = (
        '[1234, "mouse", 1.2,[[[\'size\', [123]],[\'model\',[4]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)

    # Create a fake entry.
    data_parser.player_stats_map[1234] = {'name': 'fake-mouse'}

    self.assertFalse(data_parser._ParsePlayerStats(fake_player_data))
    self.assertEquals(1, len(data_parser.player_stats_map))
    self.assertEquals('fake-mouse',
                      data_parser.player_stats_map[1234]['name'])
    self.mox.VerifyAll()

  def testParsePlayerStatsFailDuplicateStat(self):
    fake_player_data = (
        '[1234, "mouse", 1.2,[[[\'size\', [123]],[\'size\',[4]],3,'
        '\'mouse\'')

    self.mox.ReplayAll()
    data_parser = player_data_parser.PlayerDataParser(self.fake_data)
    self.assertFalse(data_parser._ParsePlayerStats(fake_player_data))
    self.assertEquals(0, len(data_parser.player_stats_map))
    self.mox.VerifyAll()


if __name__ == '__main__':
  unittest.main()
